package com.micro.controller;

import com.micro.entity.TaskInfo;
import com.micro.form.ImportForm;
import com.micro.global.TaskInfoDataBase;
import com.micro.utils.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 *
 * @auther dufengming
 *
 */
public class ImportController implements Initializable {
    @FXML
    private TextField textField;
    @FXML
    private TableView tableView;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        ImportForm.onShow(e -> {
            try {
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                Object data = clipboard.getData(DataFlavor.stringFlavor);
                if (data != null) {
                    String content = String.valueOf(data);
                    if ((StringUtils.contains(content, "http://") || StringUtils.contains(content, "http://")) && !StringUtils.contains(content, ".m3u8")) {
                        //读取网页内容
                        String webContent = HttpUtil.httpGetString(content, new HashMap<>());
                        //分割html
                        if (StringUtils.isNotBlank(webContent)) {
                            String[] splitContent = webContent.split("\n");
                            if (splitContent.length == 1) {
                                splitContent = webContent.split(" ");
                            }
                            if (splitContent.length == 1) {

                            } else {
                                int count = 0;
                                for (String s : splitContent) {
                                    if (StringUtils.contains(s, ".m3u8") && StringUtils.contains(s, "http")) {
                                        String webUrl = s.substring(s.indexOf("http"), s.lastIndexOf(".m3u8") + 5);
                                        TaskInfo taskInfo = new TaskInfo();
                                        taskInfo.setUrl(webUrl);
                                        taskInfo.setName("第" + (++count) + "集");
                                        TaskInfoDataBase.DATABASE.add(taskInfo);
                                    }
                                }
                            }
                        }
                        //解析web中m3u8
                    } else {
                        String[] strings = StringUtils.split(content, "\n");
                        int i = 0;
                        for (String string : strings) {
                            TaskInfo taskInfo = new TaskInfo();
                            if (StringUtils.contains(content, "$") || StringUtils.contains(content, "\n")) {
                                String[] str = StringUtils.split(string, "$");
                                if (str.length > 1) {
                                    if (StringUtils.contains(str[0], "http") && StringUtils.contains(str[1], ".m3u8")) {
                                        taskInfo.setUrl(str[0]);
                                        taskInfo.setName(str[1]);
                                    }
                                    if (StringUtils.contains(str[1], "http") && StringUtils.contains(str[1], ".m3u8")) {
                                        taskInfo.setUrl(str[1]);
                                        taskInfo.setName(str[0]);
                                    }
                                }
                            } else {
                                taskInfo.setUrl(string);
                                taskInfo.setName("第" + (i++) + "集");
                            }
                            if (StringUtils.isNotBlank(taskInfo.getUrl())) {
                                TaskInfoDataBase.DATABASE.add(taskInfo);
                            }
                        }
                    }
                }

            } catch (UnsupportedFlavorException unsupportedFlavorException) {
                unsupportedFlavorException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        });
    }
}
